package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetAllovertime(c *gin.Context)  {
	err, info, total := services.GetAllovertime()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func Delovertime(c *gin.Context){
	id := "-1"
	data := "-1"
	if arg := c.Query("Eno"); arg != "" {
		id = arg
		fmt.Println(id)
	}
	if id == "-1"{
		app.INFO(c,30001,"参数错误")
		return
	}
	if arg1 := c.Query("Odata"); arg1 != "" {
		data = arg1
		fmt.Println(data)
	}
	if data == "-1"{
		app.INFO(c,30002,"参数错误")
		return
	}

	err := services.Delovertime(id,data)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func Updateovertime(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	olight := com.StrTo(m["Olight"]).MustInt()
	onight := com.StrTo(m["Onight"]).MustInt()
	odata := m["Odata"]

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updateovertime(models.Overtime{ Eno: eno, Olight: olight, Onight: onight,Odata:odata})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func Addovertime(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	olight := com.StrTo(m["Olight"]).MustInt()
	onight := com.StrTo(m["Onight"]).MustInt()
	odata := m["Odata"]

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addovertime(models.Overtime{ Eno: eno, Olight: olight, Onight: onight,Odata:odata})
	fmt.Println(err)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}